//
//  VCOfDoingTest.m
//  LCOnePsychology
//
//  Created by li cheng on 16/9/7.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "VCOfDoingTest.h"
#import "ZWYNetTool.h"
#import "ChoiceOfDataDoingTest.h"
#import "DataOfDoingTest.h"
#import "RootClassOfDoingTest.h"
#import "Masonry.h"
#import "VCOfTestResult.h"
#import "Masonry.h"
#import "MJMacro.h"
#import "RootClassOfBeginDoTest.h"
#import "DataOfBeginDoTest.h"
#import "UserOfDataBeginDoTest.h"
#import "VCOfUnrealizedViewController.h"
@interface VCOfDoingTest ()
@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;
@property (strong, nonatomic) UILabel *titleLabel;
@property (strong, nonatomic) UILabel *progressLabel;
@property (strong, nonatomic) UIView *progressBackgroundView;
@property (strong, nonatomic) UIButton *fristChoiceTitleButton;
@property (strong, nonatomic) UIButton *secondChoiceTitleButton;
@property (strong, nonatomic) UIView *backViewTestSubject;
@property (nonatomic, strong) RootClassOfDoingTest *rootClassOfDoingTest;
@property (nonatomic, assign) NSInteger index;
@property (nonatomic, strong) UIView *viewOfGreenProgressBar;
@property (nonatomic, copy) NSString *stringOfChoice_ids;
@property (nonatomic, assign) NSInteger indexOfFirstWayFinalChoice;
@property (nonatomic, assign) NSInteger indexOfSecondWayFinalChoice;
@property (nonatomic, strong) UIButton *buttonOfChoice;
@property (nonatomic, strong) NSArray *arrayOfEnglishAlphabet;
@end

@implementation VCOfDoingTest

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    NSLog(@"dad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidas:%ld", _rootClassOfBeginDoTest.data.count);
    // 26 个英文字母
    _arrayOfEnglishAlphabet = @[@"A", @"B", @"C", @"D", @"E", @"F", @"G", @"H", @"I" ,@"J", @"K", @"L", @"M", @"N", @"O", @"P", @"Q", @"R", @"S", @"T", @"U", @"V", @"W", @"X", @"Y", @"Z"];
    // 创建子控件
    [self createSubView];
    // 设置 navigationItem
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@" < " style:UIBarButtonItemStylePlain target:self action:@selector(back:)];
    // 初始化 当前所做的测试题所在数组的下标
    _index = 0;
    _indexOfFirstWayFinalChoice = 0;
    _indexOfSecondWayFinalChoice = 0;
}
#pragma mark - 创建子控件
- (void)createSubView{
    //
    // 进度的 label
    _progressLabel = [[UILabel alloc] init];
    _progressLabel.textAlignment = 1;
    [_myScrollView addSubview:_progressLabel];
    [_progressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_myScrollView).offset(20);
        make.top.equalTo(_myScrollView).offset(20);
        make.width.mas_offset(50);
        make.height.mas_offset(20);
    }];
    // 进度条的底
    _progressBackgroundView = [[UIView alloc] init];
    _progressBackgroundView.backgroundColor = [UIColor grayColor];
    [_myScrollView addSubview:_progressBackgroundView];
    [_progressBackgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        // 不知道为什么, 距离 scrollview 的右边不好使
        make.left.equalTo(_progressLabel.mas_right).offset(5);
        make.centerY.equalTo(_progressLabel.mas_centerY);
        make.width.mas_offset(SCREEN_WIDTH - 20 - 50 - 5 - 20);
        make.height.mas_offset(2);
    }];
    // 创建绿色的进度条
    _viewOfGreenProgressBar = [[UIView alloc] init];
    _viewOfGreenProgressBar.backgroundColor = [UIColor greenColor];
    [_progressBackgroundView addSubview:_viewOfGreenProgressBar];
    // 测试的题目
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.numberOfLines = 0;
    [_myScrollView addSubview:_titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_myScrollView).offset(20);
        make.top.equalTo(_progressLabel.mas_bottom).offset(20);
        make.width.mas_offset(SCREEN_WIDTH - 40);
        make.height.mas_offset(50);
    }];
    // 题目的 button 的创建 在下面的 <设置子控件的数据信息> 因为那个时候有 model 值
    // 创建 backViewTestSubject 我的 frame 在下面的 <设置子控件的数据信息> 方法中设定
    _backViewTestSubject = [[UIView alloc] init];
    _backViewTestSubject.backgroundColor = [UIColor whiteColor];
    _backViewTestSubject.layer.cornerRadius = 10;
    _backViewTestSubject.clipsToBounds = YES;
    [_myScrollView addSubview:_backViewTestSubject];
}
#pragma mark - myID 的 set 方法
- (void)setMyID:(NSString *)myID{
    _myID = myID;
    // 数据处理
    [self handleData];
        NSLog(@"dad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidasdad adidas:%ld", _rootClassOfBeginDoTest.data.count);
}
#pragma mark - 点击 相对应选项 button 的方法
- (void)choiceButton:(UIButton *)button{
    NSLog(@"%d", (int)button.tag - 10000);
    // 调用计算方法
    [self computingMethod:(int)button.tag - 10000];
    // 属性赋值
    [self setSubViewData];
}
#pragma mark - 方法算法的逻辑
- (void)computingMethod:(int)indexOfChoice{
    NSString *fristChoiceQid = _rootClassOfDoingTest.data[0].choice[indexOfChoice].qid;
    // 做一个判断, 传递的 id 第一个 <是0> , 那就使用第一种方法计算
    if ([fristChoiceQid isEqualToString:@"0"]) {
        // 调用第一种方法
        [self fristComputingMethod:indexOfChoice];
        
    }
     // 做一个判断, 传递的 id 第一个就 <不是0> , 那就使用第二种方法计算
    else
    {
        // 调用第二种方法
        [self secondComputingMethod:indexOfChoice];
    };
}
#pragma mark - 第一种算法
- (void)fristComputingMethod:(int)indexOfChoice{
    NSLog(@"_index:%ld", _index);
    // 当点击的题目的个数 _index 小于 题目总的个数时, 去算出她的 id
    if (_index < _rootClassOfDoingTest.data.count - 1) {
        // 当它的题目是第一个的时候就是一个的 id
        if (_index == 0) {
        _stringOfChoice_ids = _rootClassOfDoingTest.data[0].choice[indexOfChoice].ID;
        _index++;
        // 第一次点击结束后, 让他跳出这个方法, 下面就不会再继续拼接了, 保证第一个按钮的参数就是一个
        return;
        }
        // 当他不是第一个的时候给他拼接
        else
        {
            // 取出对应第几道提里面的传递的 id, 拼接上去
            NSString *string = [NSString string];
            string = _rootClassOfDoingTest.data[_index].choice[indexOfChoice].ID;
            _stringOfChoice_ids = [NSString stringWithFormat:@"%@,%@", _stringOfChoice_ids, string];
            _index++;
        }

    }
    // 当点击的题目的个数 _index 等于 题目总的个数时
    else
    {
        if (_indexOfFirstWayFinalChoice == 0){
            // 让我们传递过去的 id _stringOfChoice_ids 去拼接最后一个传递的 id
            NSString *string = [NSString string];
            string = _rootClassOfDoingTest.data[_index].choice[indexOfChoice].ID;
            _stringOfChoice_ids = [NSString stringWithFormat:@"%@,%@", _stringOfChoice_ids, string];
            // 创建查看结果的 buttonOfLookResult
            [self creteButtonOfLookResult];
        }
        _indexOfFirstWayFinalChoice++;
        

    }
    NSLog(@"%@", _stringOfChoice_ids);
}
#pragma mark - 第二种算法
- (void)secondComputingMethod:(int)indexOfChoice{
    NSString *choiceQid = _rootClassOfDoingTest.data[_index].choice[indexOfChoice].qid;
    // 当他传递的 id 不为零的时候, 让他继续传递下去
    if (![choiceQid isEqualToString:@"0"]) {
        _stringOfChoice_ids = choiceQid;
        for (DataOfDoingTest *dataOfDoingTest in _rootClassOfDoingTest.data) {
            if ([dataOfDoingTest.ID isEqualToString:choiceQid]) {
                self.index = [dataOfDoingTest.index integerValue] - 1;
            }
        }
    }
    // 当他传递的 id 等于零的时候
    else
    {
        if (_indexOfSecondWayFinalChoice == 0) {
            // 让我们传递过去的 id _stringOfChoice_ids 等于这个选择的 id 号
            _stringOfChoice_ids = _rootClassOfDoingTest.data[_index].choice[indexOfChoice].ID;
            // 创建查看结果的 buttonOfLookResult
            [self creteButtonOfLookResult];
        }
        _indexOfSecondWayFinalChoice++;
    }
    NSLog(@"%@", _stringOfChoice_ids);
}
#pragma mark - 创建查看结果的 buttonOfLookResult
- (void)creteButtonOfLookResult{
    // 改变 scrollView 的 contentSize 的大小,
    _myScrollView.contentSize = CGSizeMake(0, _rootClassOfDoingTest.data[0].choice.count * 60 + 150 + 100);
    UIButton *buttonOfLookResult = [UIButton buttonWithType:UIButtonTypeSystem];
    [buttonOfLookResult setTitle:@"查看结果" forState:UIControlStateNormal];
    [buttonOfLookResult setTintColor:[UIColor whiteColor]];
    [buttonOfLookResult addTarget:self
                           action:@selector(lookResult:) forControlEvents:UIControlEventTouchUpInside];
    buttonOfLookResult.backgroundColor = [UIColor colorWithRed:0.180 green:0.706 blue:0.459 alpha:1.000];
    buttonOfLookResult.layer.cornerRadius = 10;
    [_myScrollView addSubview:buttonOfLookResult];
    [buttonOfLookResult mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_backViewTestSubject.mas_bottom).offset(20);
        make.left.equalTo(_myScrollView).offset(20);
        make.width.mas_offset(SCREEN_WIDTH - 40);
        make.height.mas_offset(40);
    }];
}
#pragma mark - 点击查看结果的实现方法
- (void)lookResult:(UIButton *)button{
    VCOfTestResult *vcOfTestResult = [[VCOfTestResult alloc] init];
    vcOfTestResult.stringOfhoice_ids = _stringOfChoice_ids;
    vcOfTestResult.myID = _myID;
    vcOfTestResult.rootClassOfBeginDoTest = _rootClassOfBeginDoTest;
    [self.navigationController pushViewController:vcOfTestResult animated:YES];
}
#pragma mark - 设置变化中子控件的数据信息
- (void)setSubViewData{
    // 设定变化的进度条的 frame
    _viewOfGreenProgressBar.frame = CGRectMake(0, 0, _progressBackgroundView.bounds.size.width * (_index + 1) / _rootClassOfDoingTest.data.count, 2);
    // 设置答题下面的背景色 View
    [_backViewTestSubject mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_myScrollView).offset(20);
        make.top.equalTo(_titleLabel.mas_bottom).offset(20);
        make.width.mas_offset(SCREEN_WIDTH - 40);
        make.height.mas_offset(_rootClassOfDoingTest.data[_index].choice.count * 60);
    }];
    // 我通过 for 循环去创建 button
    for (int i = 0; i <= _rootClassOfDoingTest.data[_index].choice.count - 1; i++) {

    self.buttonOfChoice = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonOfChoice.tag = 10000 + i;
        _buttonOfChoice.backgroundColor = [UIColor whiteColor];
    // 设置 button 的值
    [_buttonOfChoice setTitle:[NSString stringWithFormat:@"%@、%@", _arrayOfEnglishAlphabet[i], _rootClassOfDoingTest.data[_index].choice[i].title] forState:UIControlStateNormal];
    // 设置 button 距左边显示
    _buttonOfChoice.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    // 设置 button 文字的内部偏移量
    [_buttonOfChoice setTitleEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
    // 设置 button 的 frame
    self.buttonOfChoice.frame = CGRectMake(0, i * 60, SCREEN_WIDTH - 40, 60);
    // 给 buton 添加 点击事件
    [self.buttonOfChoice addTarget:self action:@selector(choiceButton:) forControlEvents:UIControlEventTouchUpInside];
    [_backViewTestSubject addSubview:_buttonOfChoice];
        
}
    // 进度的文字
    _progressLabel.text = [NSString stringWithFormat:@"%ld/%ld", _index + 1, _rootClassOfDoingTest.data.count];
    // 测试题目的赋值
    _titleLabel.text = _rootClassOfDoingTest.data[_index].title;
}
#pragma mark - 数据处理
- (void)handleData{
    NSString *stringURL = [NSString stringWithFormat:@"http://yiapi.xinli001.com/yi/test-question-list.json/?test_id=%@&key=e110509c8317557ef50ce777d107f4b3&v=1&_platform=android&_version=4.0&evn=1&sign=65899470067001700a70671f4f9203df&token=0d4eeb8732f9ae2c73401002570ceb93", _myID];
    [ZWYNetTool GET:stringURL andBody:nil andHeader:nil andResponse:ZWYJSON andSuccessBlock:^(id result) {
        dispatch_async(dispatch_get_main_queue(), ^{
            
            _rootClassOfDoingTest = [RootClassOfDoingTest modelDic:result];
            // 设置初始的数据
            [self initSubViewData];
        });
    } andFailureBlock:^(NSError *error) {
        
    }];
}
#pragma mark - 设置初始的数据
- (void)initSubViewData{
    // 设置绿色条的 frame
    _viewOfGreenProgressBar.frame = CGRectMake(0, 0, _progressBackgroundView.bounds.size.width / _rootClassOfDoingTest.data.count, 2);
    // 设置答题下面的背景色 View
    [_backViewTestSubject mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_myScrollView).offset(20);
        make.top.equalTo(_titleLabel.mas_bottom).offset(20);
        make.width.mas_offset(SCREEN_WIDTH - 40);
        make.height.mas_offset(_rootClassOfDoingTest.data[0].choice.count * 60);
    }];
    // 创建 button
    for (int i = 0; i <= _rootClassOfDoingTest.data[0].choice.count - 1; i++) {
        UIButton *buttonOfChoice = [UIButton buttonWithType:UIButtonTypeSystem];
        [buttonOfChoice addTarget:self action:@selector(choiceButton:) forControlEvents:UIControlEventTouchUpInside];
        buttonOfChoice.backgroundColor = [UIColor whiteColor];
        buttonOfChoice.tag = 10000 + i;
        buttonOfChoice.frame = CGRectMake(0, i * 60, SCREEN_WIDTH - 40, 60);
         // 设置 button 的文字
        [buttonOfChoice  setTitle:[NSString stringWithFormat:@"%@、%@", _arrayOfEnglishAlphabet[i], _rootClassOfDoingTest.data[0].choice[i].title] forState:UIControlStateNormal];
        // 设置 button 距左边显示
        buttonOfChoice.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        // 设置 button 文字的内部偏移量
        [buttonOfChoice setTitleEdgeInsets:UIEdgeInsetsMake(0, 20, 0, 0)];
        [_backViewTestSubject addSubview:buttonOfChoice];
    }
    // 设置开始进度 label 的值
    _progressLabel.text = [NSString stringWithFormat:@"1/%ld", _rootClassOfDoingTest.data.count];
    // 设置测试问题
    _titleLabel.text = _rootClassOfDoingTest.data[0].title;
   
    [_fristChoiceTitleButton setTitle:_rootClassOfDoingTest.data[0].choice[0].title forState:UIControlStateNormal];
    if (_rootClassOfDoingTest.data[0].choice.count == 0) {
        [_secondChoiceTitleButton setTitle:_rootClassOfDoingTest.data[0].choice[1].title forState:UIControlStateNormal];
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"这个测试题目前没有测试图片" message:@"可以试试别的" preferredStyle:UIAlertControllerStyleActionSheet];
        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        [self presentViewController:alertController animated:YES completion:^{
            
        }];
    } else {
    }
}
#pragma mark - 返回的按钮
- (void)back:(UIBarButtonItem *)baarButtonItem{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
#pragma mark - 其他
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
